package com.cheetah.start.common.utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LockLocalUtil {

    private static final Map<String, Long> map = new ConcurrentHashMap<>();

    /**
     * 锁
     *
     * @param key 锁名
     */
    public static void lock(String key, Long timestamp) {
        if (timestamp == null) {
            timestamp = System.currentTimeMillis();
        }
        map.put(key, timestamp);
    }

    /**
     * 解锁
     *
     * @param key 关键
     */
    public static void unLock(String key) {
        map.remove(key);
    }

    /**
     * 是否上锁
     *
     * @param key 关键
     * @return boolean
     */
    public synchronized static boolean isLock(String key) {
        Long startTime = map.get(key);
        if (startTime == null) {
            return false;
        } else {
            return true;
        }
    }
}
